package cn.yunhe.controller;

import cn.yunhe.entity.Assistant;
import cn.yunhe.services.IAssistantServices;
import com.github.pagehelper.Page;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 孙权庆 on 2018/6/1.
 */
@RestController
@RequestMapping("/assistant")
public class AssistantController {
    @Resource
    private IAssistantServices assistantServices;

    /**
     * 分页
     *
     * @param pageSize
     * @param pageNum
     * @param assistant
     * @return
     */
    @RequestMapping("/getPage")
    @ResponseBody
    public Map queryLikeAssistants(int pageSize, int pageNum, Assistant assistant) {
        Map map = new HashMap<>();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("assistant", assistant);
        Page page = assistantServices.queryListLikeAssistant(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;
    }

    @RequestMapping("/addAssistant")
    public int addAssistant(Assistant assistant) {
        return assistantServices.addAssistant(assistant);
    }

    /**
     * 添加辅导员页面
     *
     * @return
     */
    @RequestMapping("/toAddAssistant")
    public ModelAndView toAddAssistant() {
        return new ModelAndView("/assistant/assistant_add");
    }

    /**
     * 修改辅导员信息
     *
     * @param assistant
     * @return
     */
    @RequestMapping("/toUpdateAssistant")
    public ModelAndView toUpdateAssistant(Assistant assistant) {
        assistant = assistantServices.getAssistantById(assistant);
        ModelAndView mv = new ModelAndView();
        mv.addObject("assistant", assistant);
        mv.setViewName("/assistant/assistant_update");
        return mv;
    }

    /**
     * 删除辅导员
     *
     * @param assistant
     * @return
     */
    @RequestMapping("/deleteAssistant")
    public int deleteAssistant(Assistant assistant) {
        return assistantServices.deleteAssistant(assistant);
    }

}
